//
//  VOHMVViewController.m
//  VoiceOfHeart
//
//  Created by 吉龙 on 16/8/12.
//  Copyright © 2016年 吉龙. All rights reserved.
//

#import "VOHMVViewController.h"
#import "VOHMVView.h"
#import "VOHMVPlayViewController.h"
#import "VOHMVCollectionViewCell.h"
#import "VOHMVHeaderReusableView.h"
#import "VOHMVConnect.h"
#import "VOHMVRecommendModel.h"
@interface VOHMVViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, strong)VOHMVView *mvView;
@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, strong)UICollectionView *collection;
@property(nonatomic, strong)NSMutableDictionary *dataDic;

@end

@implementation VOHMVViewController

#pragma mark ----lazy


-(NSMutableDictionary *)dataDic{
    if (!_dataDic) {
        _dataDic = [NSMutableDictionary dictionary];
    }
    return _dataDic;
}

-(void)loadView{
    self.mvView = [[VOHMVView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    __weak typeof(self)weakSelf = self;
 
    self.mvView.topView.block = ^(UIImageView *imageView){
        VOHMVPlayViewController *playController = [[VOHMVPlayViewController alloc] init];
        [weakSelf.navigationController pushViewController:playController animated:YES];
    };
    [self loadData];
    self.mvView.topView.modelArr = self.dataDic[Officicl];
    self.view = self.mvView;
}
#pragma mark ------加载数据

-(void)loadData{
    [self recommend];
}

-(void)recommend{
    NSMutableDictionary *urlDic = [NSMutableDictionary dictionary];
    [urlDic setObject:@"0" forKey:@"D-A"];
    [urlDic setObject:@"0" forKey:@"offset"];
    [urlDic setObject:@"false" forKey:@"play"];
    [urlDic setObject:@"640*540" forKey:@"rn"];
    [urlDic setObject:@"10" forKey:@"size"];
    
   __block NSMutableArray *array = self.dataDic[Officicl];
    if (!array) {
        array = [NSMutableArray array];
    }
    
    [VOHMVConnect AFNrequestDataWithUrl:Officicl requestsHeader:RequestHeader params:urlDic HTTPMeod:@"get" sendingBlock:^(id data) {
        NSArray *dataArr = (NSArray *)data;
        for (NSDictionary *dict in dataArr) {
            VOHMVRecommendModel *model = [[VOHMVRecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [array addObject:model];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.mvView.topView.modelArr = array;
                
            });
        }
    }];
}




-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置NavigationBar
    self.mvView.topView.headerView.delegate = self;
    //设置定时器
    [self addTimer];
    //设置collection
    self.mvView.bottomView.collection.delegate =self;
    self.mvView.bottomView.collection.dataSource = self;
    [self.mvView.bottomView.collection registerClass:[VOHMVCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.mvView.bottomView.collection registerClass:[VOHMVHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    NSLog(@"%@000000000", self.dataDic[Officicl]);

}

#pragma mark -------定时器方法

-(void)addTimer{
    [self removeTimer];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}

-(void)removeTimer{
    [self.timer invalidate];
    self.timer = nil;
}


-(void)timerAction{
    static NSInteger index;
    index = self.mvView.topView.headerView.contentOffset.x / ScreenWidth;
    index ++;
    if (index == 5) {
        index = 1;
    }
   self.mvView.topView.headerView.contentOffset = CGPointMake(ScreenWidth * index, 0);
    self.mvView.topView.page.currentPage = index - 1;
}

#pragma mark------UIScrollViewDelegate

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self performSelector:@selector(addTimer) withObject:self afterDelay:2];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger indexPath = scrollView.contentOffset.x / ScreenWidth;
    if (indexPath == 5) {
        indexPath = 1;
        scrollView.contentOffset = CGPointMake(ScreenWidth *indexPath , 0);
    }else if (indexPath == 0){
        indexPath = 4;
        scrollView.contentOffset = CGPointMake(ScreenWidth *indexPath, 0);
    }
    
    self.mvView.topView.page.currentPage = indexPath - 1;

}


#pragma mark ------collectionView的实现方法

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 4;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 6;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    VOHMVCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    VOHMVHeaderReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    return headerView;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
